'VB示例:确定各月天数的笨拙做法 If (month = 1) Then days = 31 ElseIf (month = 2) Then days = 28 ElseIf (month = 3) Then days = 31 ... ElseIf (month = 11) Then days = 30 ElseIf (month = 12) Then days = 31 EndIf
While more messages toread Read a message header Decode the message ID from the message header If the message header istype1then Print a type1message Elseif the message header istype2then Print a type2message ... Elseif the message header istype19then Print a type19message Elseif the message header istype20then Print a type20message
如何修改?将动作存储到表中可以将子程序入口地址存储,类似的有多态的实现
1.2.2.5. 画向对象的方法
但是基本结构还是同样复杂
问题的逻辑可以隐藏在对象继承结构里
1 2 3 4 5 6 7 8 9 10 11 12 13 14
While more messages toread Read a message header Decode the message ID from the message header If the message header istype1then Instantiate a type1messageobject Elseif the message header istype2then Instantiate a type2messageobject ... Elseif the message header istype19then Instantiate a type19messageobject Elseif the message header istype20then Instantiate a type20messageobject Endif EndWhile
While more messages toread Read a message header Decode the message ID from the message header Look up the message description in the message-description table Read the message fields and print them based on the message description EndWhile
1.2.7. 消息打印子程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
While more fields to print Get the field type fromthe message description case ( field type ) of ( floating point ) reada floating-point value print the field label print the floating-point value of ( integer ) readanintegervalue print the field label print theintegervalue of ( characterstring ) readacharacterstring print the field label print thecharacterstring … End Case End While
' set up data for grading table Dim rangeLimit() AsDouble = { 50.0, 65.0, 75.0, 90.0, 100.0 } Dim grade() AsString = { "F", "D", "C", "B", "A" } maxGradeLevel = grade.Length - 1 ... ' assign a grade to a student based on the student's score gradeLevel = 0 studentGrade = "A" While ( gradeLevel < maxGradeLevel ) If ( studentScore < rangeLimit( gradeLevel ) ) Then studentGrade = grade( gradeLevel ) EndIf gradeLevel = gradeLevel + 1 Wend
评论